package com.yjhb.android.sbz.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jojo.android.zxlib.tool.ZxActivityTool;
import com.jojo.android.zxlib.tool.ZxDeviceTool;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.view.dialog.ILoading;
import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.UserInfo;
import com.yjhb.android.sbz.manager.im.IMManager;
import com.yjhb.android.sbz.mvp.login.MsgLoginPresenter;
import com.yjhb.android.sbz.mvp.login.MsgLoginView;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.tool.InvitationCodeTool;
import com.yjhb.android.sbz.tool.LoadingDialogTool;

import org.greenrobot.eventbus.EventBus;

import java.util.Stack;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

/**
 * 创建者 zx
 * 创建时间 2020/11/18 17:49
 */
public class VerificationCodeActivity extends BaseActivity<MsgLoginPresenter> implements MsgLoginView {
    private final String TAG = "VerificationCodeActivity";

    @BindView(R.id.getcode_tv)
    TextView getcode_tv;
    @BindView(R.id.et_code)
    EditText et_code;
    @BindView(R.id.tel_tv)
    TextView tel_tv;

    ILoading loadingDialog;

    String tel;
    private Disposable disposable;

    @Override
    protected MsgLoginPresenter initPresenter() {
        return new MsgLoginPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_verification_code;
    }

    @Override
    protected void initUI() {
        setStatusBarLightMode(true);
        loadingDialog = LoadingDialogTool.createDialog(this);
    }

    @Override
    protected void initData() {
        tel = getIntent().getStringExtra("tel");
        tel_tv.setText(tel);
        offGetCode();
        presenter.getTelCode(tel);
    }

    @Override
    protected void regEvent() {

    }

    /**
     * 关闭获取验证码
     */
    private void offGetCode() {
        getcode_tv.setEnabled(false);
        Observable.intervalRange(0, 60, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(Long aLong) {
                        getcode_tv.setText("" + (60 - aLong) + "s后重新获取");
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        getcode_tv.setEnabled(true);
                        getcode_tv.setText("获取验证码");
                    }
                });
    }

    @Override
    protected void onDestroy() {
        //停止计时器
        if (disposable != null && !disposable.isDisposed()) {
            disposable.dispose();
        }
        super.onDestroy();
    }

    /**
     * 返回键被点击
     *
     * @param view
     */
    public void onBack(View view) {
        finish();
    }

    /**
     * 确定键被点击
     *
     * @param view
     */
    public void onOk(View view) {
        if (!TextUtils.isEmpty(et_code.getText()))
            presenter.login(tel, et_code.getText().toString(), InvitationCodeTool.getInvitationCode(), ZxDeviceTool.getIMEI(this));
    }

    /**
     * 获取验证码
     *
     * @param view
     */
    public void onGetCode(View view) {
        offGetCode();
        presenter.getTelCode(tel);
    }


    @Override
    public void onMsgSend() {
        ZxToast.showCenterToastLight("验证码已发送");
    }

    @Override
    public void onLoginSuccess(UserInfo userInfo) {
        loadingDialog.cancel();
        App.updateUserInfo(userInfo);
        App.updateUUID(userInfo.getUuid());
        IMManager.getInstance().login();
        EventBus.getDefault().post(new EventMsg(EventMsg.LOGIN_SUCCESS, 2));
        startActivity(new Intent(this, MainActivity.class));
        Stack<Activity> activities = ZxActivityTool.getActivityStack();
        for (Activity activity : activities) {
            if (!(activity instanceof MainActivity))
                activity.finish();
        }
    }

    @Override
    public void loggingIn() {
        loadingDialog.show();
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        if (code == 685) {
            ZxToast.showINFOCenterToast("您的手机号未绑定，请先使用微信登录！");
            ZxActivityTool.skipActivity(this, LoginActivity.class);
        } else {
            ZxToast.showCenterToastLight(msg);
        }
    }
}
